<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Mod_record extends PNC_Model {
	/*
	 * Show record leave as annual
	 * @access public
	 * @param int
	 * @param int
	 * @return array
	 */

	public function show_record($per_page, $offset) {
		$query = $this
				->db
				->select('*')
				->from('pnc_record')
				->join('pnc_employee', 'pnc_employee.staff_id = pnc_record.staff_id')
				->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
				->join('pnc_leave_type', 'pnc_leave_type.lea_id = pnc_record.lea_id')
				->join('pnc_ss_leave', 'pnc_ss_leave.ssl_id = pnc_record.ssl_id')
				->join('pnc_sub_leave', 'pnc_sub_leave.stl_id = pnc_record.stl_id')
				->order_by('rcd_id', 'DESC')
				->limit($per_page, $offset)
				->get();
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $records) {
				$data[] = $records;
			}
			return $data;
		}
		return FALSE;
	}

	/*
	 * Show leave record type as catchup
	 * @access public
	 * @param int
	 * @param int
	 * @return array
	 */

	public function show_catchup($per_page, $offset) {
		$query = $this
				->db
				->select('*')
				->from('pnc_record')
				->join('pnc_employee', 'pnc_employee.staff_id = pnc_record.staff_id')
				->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
//				->join('pnc_group', 'pnc_employee.grp_id = pnc_group.grp_id')
				->join('pnc_leave_type', 'pnc_leave_type.lea_id = pnc_record.lea_id')
				->join('pnc_cup_des', 'pnc_record.catch_desc = pnc_cup_des.desc_id')
				->where('pnc_record.lea_id', 2)
				->order_by('rcd_id', 'DESC')
				->limit($per_page, $offset)
				->get();
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row_catchup) {
				$data[] = $row_catchup;
			}
			return $data;
		}
		return FALSE;
	}

	/* For show pending and approved for Admin */

	public function padding_approved($per_page, $offset, $type) {
		if ($type === 'pending') {
			$query = $this->db
					->select('*')
					->from('pnc_record')
					->join('pnc_employee', 'pnc_employee.staff_id = pnc_record.staff_id')
					->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
//					->join('pnc_group', 'pnc_employee.grp_id = pnc_group.grp_id')
					->join('pnc_leave_type', 'pnc_leave_type.lea_id = pnc_record.lea_id')
					->join('pnc_cup_des', 'pnc_record.catch_desc = pnc_cup_des.desc_id', 'left')
					->where('rcd_status', 0)
					->order_by('rcd_id', 'DESC')
					->limit($per_page, $offset)
					->get();
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row_padding) {
					$data[] = $row_padding;
				}
				return $data;
			}
			return FALSE;
		} else if ($type === 'approval') {
			$query = $this->db
					->select('*')
					->from('pnc_record')
					->join('pnc_employee', 'pnc_employee.staff_id = pnc_record.staff_id')
					->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
					->join('pnc_group', 'pnc_employee.grp_id = pnc_group.grp_id')
					->join('pnc_leave_type', 'pnc_leave_type.lea_id = pnc_record.lea_id')
					->join('pnc_cup_des', 'pnc_record.catch_desc = pnc_cup_des.desc_id', 'left')
					->where('rcd_status', 1)
					->order_by('rcd_id', 'DESC')
					->limit($per_page, $offset)
					->get();
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row_approve) {
					$data[] = $row_approve;
				}
				return $data;
			}
			return FALSE;
		}
	}

	/* For show all pending and approved record for all managers */

	public function approved_padding($per_page, $offset, $type, $user_id) {
		$query = '';
		if ($type === 'pending') {
			$query = $this->db
					->select('*')
					->from('pnc_record')
					->join('pnc_employee', 'pnc_employee.staff_id = pnc_record.staff_id')
					->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
					->join('pnc_group', 'pnc_employee.grp_id = pnc_group.grp_id')
					->join('pnc_leave_type', 'pnc_leave_type.lea_id = pnc_record.lea_id')
					->join('pnc_cup_des', 'pnc_record.catch_desc = pnc_cup_des.desc_id', 'left')
					->where('rcd_status', 0)
					->where('val_id', $user_id)
					->order_by('rcd_id', 'DESC')
					->limit($per_page, $offset)
					->get();
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row_padding) {
					$data[] = $row_padding;
				}
				return $data;
			}
			return FALSE;
		} else if ($type === 'approved') {
			$query = $this->db
					->select('*')
					->from('pnc_record')
					->join('pnc_employee', 'pnc_employee.staff_id = pnc_record.staff_id')
					->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
					->join('pnc_group', 'pnc_employee.grp_id = pnc_group.grp_id')
					->join('pnc_leave_type', 'pnc_leave_type.lea_id = pnc_record.lea_id')
					->join('pnc_cup_des', 'pnc_record.catch_desc = pnc_cup_des.desc_id', 'left')
					->where('rcd_status', 1)
					->where('val_id', $user_id)
					->order_by('rcd_id', 'DESC')
					->limit($per_page, $offset)
					->get();
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row_approve) {
					$data[] = $row_approve;
				}
				return $data;
			}
			return FALSE;
		} else if ($type === 'own_pending') {
			$query = $this->db
					->select('*')
					->from('pnc_record')
					->join('pnc_employee', 'pnc_employee.staff_id = pnc_record.staff_id')
					->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
					->join('pnc_group', 'pnc_employee.grp_id = pnc_group.grp_id')
					->join('pnc_leave_type', 'pnc_leave_type.lea_id = pnc_record.lea_id')
					->join('pnc_cup_des', 'pnc_record.catch_desc = pnc_cup_des.desc_id', 'left')
					->where('rcd_status', 0)
					->where('pnc_record.staff_id', $user_id)
					->order_by('rcd_id', 'DESC')
					->limit($per_page, $offset)
					->get();
			
		} else if ($type === 'own_approved') {
			$query = $this->db
					->select('*')
					->from('pnc_record')
					->join('pnc_employee', 'pnc_employee.staff_id = pnc_record.staff_id')
					->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
					->join('pnc_group', 'pnc_employee.grp_id = pnc_group.grp_id')
					->join('pnc_leave_type', 'pnc_leave_type.lea_id = pnc_record.lea_id')
					->join('pnc_cup_des', 'pnc_record.catch_desc = pnc_cup_des.desc_id', 'left')
					->where('rcd_status', 1)
					->where('pnc_record.staff_id', $user_id)
					->order_by('rcd_id', 'DESC')
					->limit($per_page, $offset)
					->get();
		}
		if ($query->num_rows() > 0) {
				foreach ($query->result() as $rows) {
					$data[] = $rows;
				}
				return $data;
			}
	}

	public function detail_record($id) {
		$query = $this->db
				->from('pnc_record')
				->join('pnc_employee', 'pnc_employee.staff_id = pnc_record.staff_id')
				->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
				->join('pnc_group', 'pnc_group.grp_id = pnc_employee.grp_id')
				->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
				->join('pnc_position', 'pnc_employee.pos_id = pnc_position.pos_id')
				->join('pnc_leave_type', 'pnc_leave_type.lea_id = pnc_record.lea_id')
				->join('pnc_ss_leave', 'pnc_ss_leave.ssl_id = pnc_record.ssl_id')
				->join('pnc_sub_leave', 'pnc_record.stl_id = pnc_sub_leave.stl_id')
				->where('rcd_id', $id)
				->get();
		return $query;
	}

	public function update_record_annual($id, $sub_type, $ss_type, $reason, $start_date, $end_date, $hour, $validator, $rcd_status, $days) {
		$data = '';
		$data = array(
			'stl_id' => $sub_type,
			'lea_reason' => $reason,
			'start_date' => $start_date,
			'end_date' => $end_date,
			'duration' => $days,
			'hour' => $hour,
			'ssl_id' => $ss_type,
			'val_id' => $validator,
			'rcd_status' => $rcd_status
		);
		$this->db->where('rcd_id', $id);
		return $this->db->update('pnc_record', $data);
	}

	public function detail_catchup($id) {
		$query = $this
				->db
				->select('*')
				->from('pnc_record')
				->join('pnc_employee', 'pnc_employee.staff_id = pnc_record.staff_id')
				->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
				->join('pnc_group', 'pnc_employee.grp_id = pnc_group.grp_id')
				->join('pnc_leave_type', 'pnc_leave_type.lea_id = pnc_record.lea_id')
				->join('pnc_cup_des', 'pnc_record.catch_desc = pnc_cup_des.desc_id')
				->where('pnc_leave_type.lea_id', 2)
				->where('rcd_id', $id)
				->get();
		return $query;
	}

	public function update_record_catchup($rcd_id, $catch_id, $desc, $reason, $start_date, $end_date, $work_on, $catch_on, $validator, $rcd_status, $hour, $days) {
		$data = array(
			'lea_reason' => $reason,
			'start_date' => $start_date,
			'end_date' => $end_date,
			'val_id' => $validator,
			'rcd_status' => $rcd_status,
			'duration' => $days,
			'hour' => $hour
		);
		$catch_up = array(
			'description' => $desc,
			'working_on' => $work_on,
			'catch_on' => $catch_on
		);
		$this->db->where('desc_id', $catch_id);
		$this->db->update('pnc_cup_des', $catch_up);
		$this->db->where('rcd_id', $rcd_id);
		return $this->db->update('pnc_record', $data);
	}

	public function delete_once($id, $where, $table, $desc_id) {
		if ($desc_id) {
			$this->db->where('desc_id', $desc_id);
			$del_desc = $this->db->delete('pnc_cup_des');
			if ($del_desc) {
				$this->db->where($where, $id);
				return $this->db->delete($table);
			}
		} else {
			$this->db->where($where, $id);
			return $this->db->delete($table);
		}
	}

//	public function delete_once($id, $where, $table) {
//		$this->db->where($where, $id);
//		return $this->db->delete($table);
//	}

	public function approve_update($id, $cur_status, $field, $table) {
//		echo $id, $cur_status, $field, $table; die();
		$data = '';
		if ($cur_status == 1) {
			$data = array($field => '0');
		} else {
			$data = array($field => '1');
		}
		$this->db->where('rcd_id', $id);
		$result = $this->db->update($table, $data);
		return $this->db->affected_rows($result);
	}

}

/* End of file mod_record.php */
/* Location: ./application/models/mod_record.php */